package tech.aister.controller.client;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aister.pojo.Book;
import tech.aister.pojo.Category;
import tech.aister.pojo.Msg;
import tech.aister.pojo.User;
import tech.aister.service.BookService;
import tech.aister.service.CategoryService;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 全部采用的分页，List<Book>返回PageInfo,
 * pageSize全局定义
 */
@Controller("ClientBookController")
@RequestMapping("/client") //webapp
public class BookController {
	private final int pageSize=6;

	@Autowired
	private BookService service;
	@Autowired
    private CategoryService categoryService;


	@RequestMapping("/getAllBook")//接收浏览器的需求
	//得到购物车中的全部的图书（函数getAllbook在manager中BookController）
	public String getAllBookAndCategory(@RequestParam(defaultValue="1")int pageNum, Model model){
		PageInfo<Book> pageInfo=service.getAllBook(pageNum,pageSize);
		List<Category> categories=categoryService.getAllCategory();
		model.addAttribute("pageInfo",pageInfo);  //往前台传数据，可以传对象
		model.addAttribute("categories",categories);
		model.addAttribute("url","getAllBook");
		return "client/body";//展示图书
	}


	@RequestMapping("/getAllBookByCategory")
	//查询购物车中的书
	public String getAllBookByCategory(@RequestParam String category_id,
                                       @RequestParam(defaultValue="1")int pageNum, Model model){
		PageInfo<Book> pageInfo=service.getAllBookByCategory(category_id,pageNum,pageSize);
	    List<Category> categories=categoryService.getAllCategory();
		model.addAttribute("pageInfo",pageInfo);
		model.addAttribute("categories",categories);
		model.addAttribute("url","getAllBookByCategory");
		return "client/body";
	}

	@RequestMapping("/queryBook")
	public String queryBook(Book book, @RequestParam(defaultValue="1")int pageNum, Model model) {
		PageInfo<Book> pageInfo=service.queryBook(book, pageNum,pageSize);
	    List<Category> categories=categoryService.getAllCategory();
		model.addAttribute("pageInfo",pageInfo);
		model.addAttribute("categories",categories);
		model.addAttribute("url","queryBook");
		model.addAttribute("query_name",book.getName());
		return "client/body";
	}

	@RequestMapping("/findBookById")
	public String findBookById(@RequestParam String id, Model model) {
		Book book=service.findBookById(id);//找书的id号
		model.addAttribute("book",book);

	     return "client/bookDetail";//买书确定按钮
	}

	@Controller
	@RequestMapping("/client")
	public static class UserController {

		@Autowired
		private UserService service;

		//登录
		@RequestMapping("/login")
		@ResponseBody
		//接收的参数绑定在函数的参数上
		//判断登录账号和密码是否正确
		public Msg login(HttpSession session, @RequestParam String username, @RequestParam String password) {

			User user=service.login(username,password);
			 if(user==null) {
				 return new Msg(0,"用户名或密码错误，登陆失败");
			 }else {
				 session.setAttribute("user", user);//给user设置session
				return  new Msg(1,"登陆成功");
			 }
		}

		/*
		  * 注册
		  *判断该用户名是否存在、若存在则应该换一个名、不存在就注册
		 */
		@RequestMapping("/regist")
		@ResponseBody//将java对象转为json格式的数据。
		//注册
		public Msg regist(User user, Model model) {
			 if(!service.isUsernameExist(user.getUsername())) {
				 service.regist(user);
				return new Msg(1,"注册成功");
			}else {
				 return new Msg(0,"该用户名已被注册，请重新填写");
			 }
		}


		@RequestMapping("/loginout")
		//对应安全退出的按钮
		public String loginout(HttpSession session) {
			session.invalidate();
			return "index";
		}
	}
}
